// 用于拼接字符串（自动插入换行）并替换到代码文件的类
function myString(dfText) {
    this.val = dfText || ''
    this.add = text => (this.val += (text || '') + '\r\n')
    this.writeTo = (strCode,tag) => {
        return strCode.replace(new RegExp(`\\@${tag},?`, 'g'), this.val.trimLeft())
    }
}

function loadExField(tables,setting,filedCodeEx,deep){
    tables.forEach((table,index) =>{
        var alias =`t${deep}${index}`
        setting.qTables.push({table:table,alias:alias})
        table.columns.forEach(column =>{
            const name = `${window.$upFirst(table.className)}_${window.$upFirst(column.name)}` 

            filedCodeEx.add(`        /// <summary>`)
            filedCodeEx.add(`        ///${column.coment || name}`)
            filedCodeEx.add(`        /// </summary>`)
            filedCodeEx.add(`        [Column(IsIgnore = true)]`)
            if(['string','text'].includes(column.csType)){
                filedCodeEx.add(`        public string ${name} { get; set; }`)
            }
            else{
                filedCodeEx.add(`        public ${column.csType}? ${name} { get; set; }`)
            }
            
            if(['DateTime'].includes(column.csType)){
                filedCodeEx.add(`        /// <summary>`)
                filedCodeEx.add(`        /// ${column.coment || name}(时间查询范围)`)
                filedCodeEx.add(`        /// </summary>`)
                filedCodeEx.add(`        [Column(IsIgnore = true)]`)
                filedCodeEx.add(`        public string ${name}QRange { get; set; }`)
            }
        })

        loadExField(setting.dbTable.filter(t=>t.relationName == table.name && t.relationType == '1'),setting,filedCodeEx,deep+1)
    })
}

// 生成实体代码
export const generatorEntity = (setting,temp,table)=>{
    let strCode = temp
    temp = null

    strCode = strCode.replace('@Namespace',setting.namespace.entity)
    strCode = strCode.replace('@ProjectName',`${setting.project.f_Name}-${setting.moduleName}`)
    strCode = strCode.replace('@CompanyName',setting.company)
    strCode = strCode.replace('@CreateUserName',setting.userInfo.f_RealName)
    strCode = strCode.replace('@Datetime',setting.createTime)

    if(table.comment){
        strCode = strCode.replace('@Des',`${table.name}(${table.comment})表的实体`)
    }
    else{
        strCode = strCode.replace('@Des',`${table.name}表的实体`)
    }
    strCode = strCode.replace('@Table',table.name)
    strCode = strCode.replace('@EntityName',window.$upFirst(`${table.className}Entity`))


    // 生成字段
    var filedCode = new myString()   // 表字段
    var filedCodeEx = new myString() // 扩展字段
    table.columns.forEach(column => {
        const name = window.$upFirst(column.name)

        filedCode.add(`        /// <summary>`)
        filedCode.add(`        /// ${column.coment || name}`)
        filedCode.add(`        /// </summary>`)

        if(column.isPrimary && !column.isIdentity){
            filedCode.add(`        [Column(IsPrimary = true)]`)
        }
        else if(column.isPrimary && column.isIdentity){
            filedCode.add(`        [Column(IsPrimary = true,IsIdentity = true)]`)
        }
        else if(column.isIdentity){
            filedCode.add(`        [Column(IsIdentity = true)]`)
        }

        if(['string','text'].includes(column.csType)){
            filedCode.add(`        public string ${name} { get; set; }`)
        }
        else{
            filedCode.add(`        public ${column.csType}? ${name} { get; set; }`)
        }

        if(['DateTime'].includes(column.csType)){
            filedCodeEx.add(`        /// <summary>`)
            filedCodeEx.add(`        /// ${column.coment || name}(时间查询范围)`)
            filedCodeEx.add(`        /// </summary>`)
            filedCodeEx.add(`        [Column(IsIgnore = true)]`)
            filedCodeEx.add(`        public string ${name}QRange { get; set; }`)
        }

    })
    strCode = filedCode.writeTo(strCode,'Fields')

    if(table.type == 'main'){
        // 1对1
        setting.qTables.push({table:table,alias:'t'})
        loadExField(setting.dbTable.filter(t=>t.relationName == table.name && t.relationType == '1'),setting,filedCodeEx,1)
    }
    strCode = filedCodeEx.writeTo(strCode,'ExFields')
    
    return strCode;
}